package com.yantong.controller.api.baseapi;

import com.yantong.constant.BusinessCode;
import com.yantong.result.ResultVo;
import com.yantong.utils.AliMsmUtils;
import com.yantong.utils.CommonUtils;
import com.yantong.utils.ValidateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.concurrent.TimeUnit;

/**
 *  手机号码
 * @author 李一凡
 */
@Api(tags = "通用手机号码相关")
@Controller
@RequestMapping("/baseapi/mobile")
public class ApiMobileController {

    @Autowired
    private StringRedisTemplate redisTemplate;


    /**
     * 发送手机号验证码
     * @param mobile
     * @param code
     * @return
     */
    @ApiOperation(value = "发送手机号验证码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mobile", value = "手机号", defaultValue = "", dataType = "String", required = true),
            @ApiImplicitParam(name = "code", value = "短信发送授权码(1.h5登录 code)", defaultValue = "", dataType = "String", required = true),
    })
    @ResponseBody
    @GetMapping("/sendRegisterCode")
    public ResultVo sendRegisterCode(String mobile, String code) {
        //验证手机号码是否正确
        if(!ValidateUtil.validatePhone(mobile)){
            return ResultVo.error("手机号码有误");
        }
        String aCode = redisTemplate.opsForValue().get(code);
        if(StringUtils.isEmpty(aCode)){
            return ResultVo.error(BusinessCode.VERIFYCODE_ERROR);
        }
        redisTemplate.delete(code);
        //生成四位短信验证码
        String regisCode = CommonUtils.generateNumberRandomStr(4);
        boolean b = AliMsmUtils.sendRegistCode(mobile,regisCode);

        if(b){
            redisTemplate.opsForValue().set("regisCode"+mobile,regisCode,5, TimeUnit.MINUTES);
            return ResultVo.ok();
        }
        return ResultVo.error("短信发送失败");
    }


    /**
     * 发送手机号验证码
     * @param mobile
     * @param registerCode
     * @return
     */
    @ApiOperation(value = "验证手机号验证码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mobile", value = "手机号", defaultValue = "", dataType = "String", required = true),
            @ApiImplicitParam(name = "registerCode", value = "短信验证码", defaultValue = "", dataType = "String", required = true),
    })
    @ResponseBody
    @GetMapping("/verifyRegisterCode")
    public ResultVo verifyRegisterCode(String mobile, String registerCode) {
        String aCode = redisTemplate.opsForValue().get("regisCode"+mobile);
        //验证码不存在 , 直接返回错误
        if(StringUtils.isEmpty(aCode)){
            return ResultVo.error();
        }

        if(registerCode.equals(aCode)){
            //验证码验证成功 , 存入标识代表手机号码验证成功是本人进行操作
            redisTemplate.opsForValue().set("sMobile"+mobile,"1",5, TimeUnit.MINUTES);
            return ResultVo.ok();
        }
        return ResultVo.error(BusinessCode.VERIFYCODE_ERROR);
    }


}
